#include "log_capture.h"
#include <iostream>

namespace wlogger {

LogCapture::LogCapture(const Logger::Level level, const std::string& file, const uint32_t line,
                       const std::string& function, const std::wstring& check_expression)
    : level_(level), file_(UTF8ToUnicode(file)), line_(std::to_wstring(line)), function_(UTF8ToUnicode(function)), check_expression_(check_expression) {
      // std::cout << "LogCapture" <<std::endl;
}

std::wostringstream& LogCapture::stream() {
  // std::cout << "sstream_" <<std::endl;
  return sstream_;
}

LogCapture::~LogCapture() {
  if (level_ == Logger::Level::FATAL_LEVEL) {
    if (!check_expression_.empty()) {
      sstream_ << L"\n\tCHECK(" + check_expression_ + L") fail.";
    }
  }

  // std::cout << "level_:" << (int)level_ <<std::endl;
  std::wostringstream tmp;
  tmp << L"[";
  tmp << file_;
  tmp << L":";
  tmp << line_;
  tmp << L"][";
  tmp << function_;
  tmp << L"] ";
  tmp << sstream_.str();

  // std::wcout << tmp.str();
  Logger::Instance()->Log(level_, tmp.str().c_str());
}

}  // namespace logger
